package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiyun.mapper.UserMapper;
import com.jiyun.pojo.XUser;
import com.jiyun.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

@Slf4j
@Service("uacClientDetailsService")
public class UacClientDetailsServiceImpl implements /*UacClientDetailsService,*/ ClientDetailsService {

    //@Resource
    //private IDemandInfoService demandInfoService;

    //@Resource
    //private IDemandBalanceInfoService demandBalanceInfoService;

    /*@Resource
    UacTokenProperties uacTokenProperties;*/

    @Resource
    PasswordEncoder passwordEncoder;
    @Resource
    UserService userService;

    //@Resource
    //private LaunchFeignClient launchFeignClient;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        LambdaQueryWrapper<XUser> qw = new LambdaQueryWrapper<com.jiyun.pojo.XUser>();
        qw.eq(com.jiyun.pojo.XUser::getUsername, clientId);
        com.jiyun.pojo.XUser one = userService.getOne(qw);
        if (!one.getUsername().equals(clientId)) {
            return null;
        }
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId(one.getUsername());
        clientDetails.setClientSecret(passwordEncoder.encode(one.getPassword()));
        clientDetails.setScope(Arrays.asList("all"));
        clientDetails.setAccessTokenValiditySeconds(24*600);
        return clientDetails;
    }

}
